package com.hrbxlh.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.VirtualMachineManageActionImpl;
import com.hrbxlh.exception.OpenstackException;
import com.hrbxlh.exception.VirtualMachineAllocateException;
import com.hrbxlh.util.ConfigFileUtil;
import com.hrbxlh.websocket.VirtualMachineShareWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：VirtualMachineManageAction.java
 * Description：虚拟机管理action类
 * History：
 * Date           Author               Desc
 * 2017.5.2		  王晨                 建立
 * -------------------------------------------------------
 */

@Controller
public class VirtualMachineManageAction {

    @Autowired
    @Qualifier("virtualMachineManageActionImpl")
    private VirtualMachineManageActionImpl virtualMachineManageActionImpl;

    @Autowired
    @Qualifier("courseActionImpl")
    private CourseActionImpl courseActionImpl;

    private Logger logger = LoggerFactory.getLogger(VirtualMachineManageAction.class);

    /**
     * @description 跳转到虚机查询页面 (左侧导航栏、分页查询、条件查询、tab页切换）
     * @param jsonStr pageSize
     *                pageNow
     *                forceUpdate 与云平台同步
     *                status 虚拟机状态
     * @param request
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/skipVirtualMachineManage", method = RequestMethod.GET)
    public ModelAndView skipVirtualMachineManage(String jsonStr, HttpServletRequest request) throws IOException {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放返回值
        Map<String, Object> returnMap = new HashMap<String, Object>();
        //返回字符串
        String reJsonStr;
        //存放页面
        String pageName;
        //存放结果值
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取参数值
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        try {
            if (jsonStr != null) {
                preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
            }
            //获取虚拟机的所有状态
            List<Map<String, Object>> statusList = virtualMachineManageActionImpl.getVirtualMachineStatusList();
            resultMap.put("statusList", statusList);
            //返回虚拟机状态
            if (preParamMap.get("status") != null) {
                resultMap.put("status", preParamMap.get("status"));
            }
            //每页条数
            resultMap.put("pageSize", preParamMap.get("pageSize"));
            //获取我的消息
            List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
            resultMap.put("noticeList", noticeList);
            //从云平台同步虚拟机列表和虚拟机模板列表
            if (preParamMap.get("forceUpdate") != null && (Boolean) preParamMap.get("forceUpdate")) {
                virtualMachineManageActionImpl.updateVirtualMachineList();
                virtualMachineManageActionImpl.updateFlavor();
            }
            //获取虚拟机列表
            Map<String, Object> virtualMachineListMap = virtualMachineManageActionImpl.getVirtualMachineList(preParamMap);
            resultMap.put("virtualMachineList", virtualMachineListMap.get("virtualMachineList"));
            resultMap.put("totalPage", virtualMachineListMap.get("totalPage"));
            resultMap.put("pageNow", virtualMachineListMap.get("pageNow"));
            //获取虚拟机总数、已分配虚机、未分配虚机
            Map<String, Object> virtualMachineNumMap = virtualMachineManageActionImpl.getVirtualMachineNum();
            resultMap.put("vmTotalNum", virtualMachineNumMap.get("vmTotalNum"));
            resultMap.put("vmAllocatedNum", virtualMachineNumMap.get("vmAllocatedNum"));
            resultMap.put("vmUnAllocatedNum", virtualMachineNumMap.get("vmUnAllocatedNum"));
            //虚机管理列表页面
            pageName = "pc/virtualMachineManage";
        } catch (OpenstackException e) {
            try {
                //返回异常提示
                resultMap.put("errMsg", "与云平台同步出现异常，当前显示虚拟机列表可能不是有效列表，建议检查系统的网络情况");
                logger.error("与云平台同步出现异常", e);
                //获取虚拟机列表
                Map<String, Object> virtualMachineListMap = virtualMachineManageActionImpl.getVirtualMachineList(preParamMap);
                resultMap.put("virtualMachineList", virtualMachineListMap.get("virtualMachineList"));
                resultMap.put("totalPage", virtualMachineListMap.get("totalPage"));
                resultMap.put("pageNow", virtualMachineListMap.get("pageNow"));
                //获取虚拟机总数、已分配虚机、未分配虚机
                Map<String, Object> virtualMachineNumMap = virtualMachineManageActionImpl.getVirtualMachineNum();
                resultMap.put("vmTotalNum", virtualMachineNumMap.get("vmTotalNum"));
                resultMap.put("vmAllocatedNum", virtualMachineNumMap.get("vmAllocatedNum"));
                resultMap.put("vmUnAllocatedNum", virtualMachineNumMap.get("vmUnAllocatedNum"));
                //虚机管理列表页面
                pageName = "pc/virtualMachineManage";
            } catch (Exception e1) {
                e1.printStackTrace();
                pageName = "public/pageerror";
            }
        } catch (Exception e) {
            resultMap.put("errMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error("跳转虚机查询页面出现异常", e);
            pageName = "public/pageerror";
        } finally {
            reJsonStr = mapper.writeValueAsString(resultMap);
            returnMap.put("result", reJsonStr);
            System.out.println("返回的  :" + reJsonStr);
        }
        return new ModelAndView(pageName, returnMap);
    }

    /**
     * @description 跳转到教师虚机分配页面 (分页查询、条件查询、tab页切换）
     * @param jsonStr pageSize
     *                pageNow
     *                departmentId
     * @param request
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/skipVirtualMachineManageTeacher", method = RequestMethod.GET)
    public ModelAndView skipVirtualMachineManageTeacher(String jsonStr, HttpServletRequest request) throws IOException {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放返回值
        Map<String, Object> returnMap = new HashMap<String, Object>();
        //返回字符串
        String reJsonStr;
        //存放页面
        String pageName;
        //存放结果值
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取参数值
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        try {
            if (jsonStr != null) {
                preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
            }
            //获取学院列表
            List<Map<String, Object>> departmentList = virtualMachineManageActionImpl.getDepartmentList();
            resultMap.put("departmentList", departmentList);
            //返回学院ID
            if (preParamMap.get("departmentId") != null) {
                resultMap.put("departmentId", preParamMap.get("departmentId"));
            }
            //每页条数
            resultMap.put("pageSize", preParamMap.get("pageSize"));
            //获取我的消息
            List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
            resultMap.put("noticeList", noticeList);
            //获取教师列表
            Map<String, Object> teacherListMap = virtualMachineManageActionImpl.getTeacherList(preParamMap);
            resultMap.put("teacherList", teacherListMap.get("teacherList"));
            resultMap.put("totalPage", teacherListMap.get("totalPage"));
            resultMap.put("pageNow", teacherListMap.get("pageNow"));
            //获取虚拟机模板列表
            List<Map<String, Object>> flavorList = virtualMachineManageActionImpl.getFlavorList();
            resultMap.put("flavorList", flavorList);
            //教师分配虚机列表页面
            pageName = "pc/virtualMachineManageTeacher";
        } catch (Exception e) {
            resultMap.put("errMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error("跳转教师虚机分配页面出现异常", e);
            pageName = "public/pageerror";
        } finally {
            reJsonStr = mapper.writeValueAsString(resultMap);
            returnMap.put("result", reJsonStr);
            System.out.println("返回的  :" + reJsonStr);
        }
        return new ModelAndView(pageName, returnMap);
    }

    /**
     * @description 跳转到学生虚机分配页面 (分页查询、条件查询、tab页切换）
     * @param jsonStr pageSize
     *                pageNow
     *                departmentId
     * @param request
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/skipVirtualMachineManageClass", method = RequestMethod.GET)
    public ModelAndView skipVirtualMachineManageClass(String jsonStr, HttpServletRequest request) throws IOException {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放返回值
        Map<String, Object> returnMap = new HashMap<String, Object>();
        //返回字符串
        String reJsonStr;
        //存放页面
        String pageName;
        //存放结果值
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取参数值
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        try {
            if (jsonStr != null) {
                preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
            }
            //获取学院列表
            List<Map<String, Object>> departmentList = virtualMachineManageActionImpl.getDepartmentList();
            resultMap.put("departmentList", departmentList);
            //返回学院ID
            if (preParamMap.get("departmentId") != null) {
                resultMap.put("departmentId", preParamMap.get("departmentId"));
            }
            //每页条数
            resultMap.put("pageSize", preParamMap.get("pageSize"));
            //获取我的消息
            List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
            resultMap.put("noticeList", noticeList);
            //获取班级列表
            Map<String, Object> classListMap = virtualMachineManageActionImpl.getClassList(preParamMap);
            resultMap.put("classList", classListMap.get("classList"));
            resultMap.put("totalPage", classListMap.get("totalPage"));
            resultMap.put("pageNow", classListMap.get("pageNow"));
            //获取虚拟机模板列表
            List<Map<String, Object>> flavorList = virtualMachineManageActionImpl.getFlavorList();
            resultMap.put("flavorList", flavorList);
            //学生分配虚机列表页面
            pageName = "pc/virtualMachineManageClass";
        } catch (Exception e) {
            resultMap.put("errMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error("跳转学生分配虚机页面出现异常", e);
            pageName = "public/pageerror";
        } finally {
            reJsonStr = mapper.writeValueAsString(resultMap);
            returnMap.put("result", reJsonStr);
            System.out.println("返回的  :" + reJsonStr);
        }
        return new ModelAndView(pageName, returnMap);
    }

    /**
     * @description 删除虚拟机
     * @param jsonStr vmId
     * @return
     */
    @RequestMapping(value = "/deleteVirtualMachine", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteVirtualMachine(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = virtualMachineManageActionImpl.deleteVirtualMachine(preParamMap);
        System.out.println("返回的：" + mapper.writeValueAsString(resultMap));
        return resultMap;
    }

    /**
     * @description 获取虚拟机总数、已分配虚机、未分配虚机
     * @return
     */
    @RequestMapping(value = "/getVirtualMachineNum", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getVirtualMachineNum() {
        return virtualMachineManageActionImpl.getVirtualMachineNum();
    }

    /**
     * @description 教师分配虚拟机
     * @param jsonStr stuorteaId
     *                hostNum
     *                hostCpu
     *                hostRam
     *                hostDisk
     *                slaveNum
     *                slaveCpu
     *                slaveRam
     *                slaveDisk
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/allocateVirtualMachineTeacher", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> allocateVirtualMachineTeacher(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            preParamMap.put("isTeacher", true);
            virtualMachineManageActionImpl.allocateVirtualMachine(preParamMap);
            resultMap.put("result", "success");
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 教师解除分配虚拟机提交
     * @param jsonStr stuorteaId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/unAllocateVirtualMachineTeacher", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unAllocateVirtualMachineTeacher(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            virtualMachineManageActionImpl.unallocateVirtualMachineTeacher(preParamMap);
            resultMap.put("result", "success");
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 教师删除虚拟机提交
     * @param jsonStr stuorteaId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/deleteVirtualMachineTeacher", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteVirtualMachineTeacher(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            virtualMachineManageActionImpl.deleteVirtualMachineTeacher(preParamMap);
            resultMap.put("result", "success");
        } catch (OpenstackException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 获取教师虚拟机详情
     * @param jsonStr stuorteaId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getVirtualMachineTeacherDetail", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getVirtualMachineTeacherDetail(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Map<String, Object>> virtualMachineList = virtualMachineManageActionImpl.getVirtualMachineByStuorteaId(preParamMap);
            Map<String, Object> teacherDetail = virtualMachineManageActionImpl.getTeacherDetail(preParamMap);
            resultMap.put("result", "success");
            resultMap.put("virtualMachineList", virtualMachineList);
            resultMap.put("stuorteaId", preParamMap.get("stuorteaId"));
            resultMap.put("userName", teacherDetail.get("userName"));
            resultMap.put("headIcon", teacherDetail.get("headIcon"));
            String[] keysArray = {"server_address", "project_name"};
            Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
            resultMap.put("imageUrl", (String) urlmap.get("server_address") + urlmap.get("project_name") + "images/pc/");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 替换虚拟机提交
     * @param jsonStr stuorteaId
     *                vmId
     *                broken
     *                msg
     *                host
     *                cpu
     *                ram
     *                disk
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/replaceVirtualMachine", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> replaceVirtualMachine(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Map<String, Object> virtualMachine = virtualMachineManageActionImpl.replaceVirtualMachine(preParamMap);
            resultMap.put("result", "success");
            resultMap.put("virtualMachine", virtualMachine);
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 班级分配虚拟机提交
     * @param jsonStr classId
     *                allocateNum
     *                hostNum
     *                hostCpu
     *                hostRam
     *                hostDisk
     *                slaveNum
     *                slaveCpu
     *                slaveRam
     *                slaveDisk
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/allocateVirtualMachineClass", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> allocateVirtualMachineClass(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //记录成功分配的学生数量
        int allocatedNum = 0;
        //管理员请求分配的学生数量
        int allocateNum = (Integer) preParamMap.get("allocateNum");
        try {
            preParamMap.put("isClass", true);
            List<Map<String, Object>> stuList = virtualMachineManageActionImpl.getStuUnallocatedList(preParamMap);
            if (allocatedNum < stuList.size()) {
                stuList = stuList.subList(0, allocateNum);
            }
            for (Map<String, Object> student : stuList) {
                String stuorteaId = (String) student.get("stuorteaId");
                preParamMap.put("stuorteaId", stuorteaId);
                virtualMachineManageActionImpl.allocateVirtualMachine(preParamMap);
                allocatedNum++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (allocatedNum > 0) {
                resultMap.put("result", "success");
            } else {
                resultMap.put("result", "fail");
            }
            resultMap.put("msg", "分配成功" + allocatedNum + "个学生," + "失败" + (allocateNum - allocatedNum) + "个学生");
        }
        return resultMap;
    }

    /**
     * @description 班级解除分配虚拟机提交
     * @param jsonStr classId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/unAllocateVirtualMachineClass", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unAllocateVirtualMachineClass(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            virtualMachineManageActionImpl.unallocateVirtualMachineClass(preParamMap);
            resultMap.put("result", "success");
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 班级删除分配虚拟机提交
     * @param jsonStr classId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/deleteVirtualMachineClass", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteVirtualMachineClass(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            virtualMachineManageActionImpl.deleteVirtualMachineClass(preParamMap);
            resultMap.put("result", "success");
        } catch (OpenstackException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (VirtualMachineAllocateException e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
            resultMap.put("msg", "系统异常");
        }
        return resultMap;
    }

    /**
     * @description 跳转到班级虚机详情页面
     * @param jsonStr pageNow
     *                pageSize
     *                classId
     * @param request
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/skipVirtualMachineClass", method = RequestMethod.GET)
    public ModelAndView skipVirtualMachineClass(String jsonStr, HttpServletRequest request) throws IOException {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        //存放返回值
        Map<String, Object> returnMap = new HashMap<String, Object>();
        //返回字符串
        String reJsonStr;
        //存放页面
        String pageName;
        //存放结果值
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取参数值
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        try {
            if (jsonStr != null) {
                preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
            }
            //每页条数
            resultMap.put("pageSize", preParamMap.get("pageSize"));
            //班级ID
            resultMap.put("classId", preParamMap.get("classId"));
            //获取我的消息
            List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
            resultMap.put("noticeList", noticeList);
            //查询班级名称，专业名称
            Map<String, Object> classDetailMap = virtualMachineManageActionImpl.getClassDetail(preParamMap);
            resultMap.put("className", classDetailMap.get("className"));
            resultMap.put("professionName", classDetailMap.get("professionName"));
            //查询班级虚拟机详情列表
            Map<String, Object> stuListMap = virtualMachineManageActionImpl.getVirtualMachineListByClass(preParamMap);
            resultMap.put("stuList", stuListMap.get("stuList"));
            String[] keysArray = {"server_address", "project_name"};
            Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
            resultMap.put("imageUrl", (String) urlmap.get("server_address") + urlmap.get("project_name") + "images/pc/");
            resultMap.put("totalPage", stuListMap.get("totalPage"));
            resultMap.put("pageNow", stuListMap.get("pageNow"));
            //获取虚拟机模板列表
            List<Map<String, Object>> flavorList = virtualMachineManageActionImpl.getFlavorList();
            resultMap.put("flavorList", flavorList);
            //班级虚机详情列表页面
            pageName = "pc/virtualMachineClass";
        } catch (Exception e) {
            resultMap.put("errMsg", "跳转页面失败");
            e.printStackTrace();
            logger.error("跳转班级虚机详情页面出现异常", e);
            pageName = "public/pageerror";
        } finally {
            reJsonStr = mapper.writeValueAsString(resultMap);
            returnMap.put("result", reJsonStr);
            System.out.println("返回的  :" + reJsonStr);
        }
        return new ModelAndView(pageName, returnMap);
    }

    /**
     * @description 教师查询实验共享成员列表
     * @param jsonStr courseId
     *                stuorteaId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getTeacherShareMemberList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTeacherShareMemberList(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Map<String, Object>> shareMemberList = virtualMachineManageActionImpl.getTeacherShareMemberList(preParamMap);
            resultMap.put("shareMemberList", shareMemberList);
            resultMap.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
        }
        return resultMap;
    }

    /**
     * @description 学生查询实验共享成员列表
     * @param jsonStr courseId
     *                stuorteaId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getStudentShareMemberList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getStudentShareMemberList(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap = virtualMachineManageActionImpl.getStudentShareMember(preParamMap);
            resultMap.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
        }
        return resultMap;
    }

    /**
     * @description 共享实验
     * @param jsonStr stuorteaId
     *                vmId
     *                shareUserIdList
     *                marks
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/shareVirtualMachine", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> shareVirtualMachine(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String vmId = (String) preParamMap.get("vmId");
            String stuorteaId = (String) preParamMap.get("stuorteaId");
            String userName = virtualMachineManageActionImpl.getUserName(stuorteaId);
            List<String> shareStuorteaIdList = (List<String>) preParamMap.get("shareUserIdList");
            VirtualMachineShareWebSocket.broadcast(userName, vmId, shareStuorteaIdList, (Integer) preParamMap.get("marks"));
            resultMap.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
        }
        return resultMap;
    }

    /**
     * @param jsonStr
     * @return
     * @description 分页、条件查询虚机
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getVirtualMachineList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getVirtualMachineList(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        return virtualMachineManageActionImpl.getVirtualMachineList(preParamMap);
    }

    /**
     * @param jsonStr
     * @return
     * @description 分页、条件查询教师列表
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getTeacherVirtualMachineList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTeacherVirtualMachineList(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        return virtualMachineManageActionImpl.getTeacherList(preParamMap);
    }

    /**
     * @param jsonStr
     * @return
     * @description 分页、条件查询班级列表
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getClassVirtualMachineList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getClassVirtualMachineList(String jsonStr) throws Exception {
        //转换json格式工具
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> preParamMap = new HashMap<String, Object>();
        if (jsonStr != null) {
            preParamMap = mapper.readValue(jsonStr, preParamMap.getClass());
        }
        return virtualMachineManageActionImpl.getClassList(preParamMap);
    }
}
